package com.efast.dmom.mgmt.service.les.impl;

import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.bean.les.pda.PdaQueryTwInvPacktranDetail;
import com.efast.dmom.mgmt.dao.les.*;
import com.efast.dmom.mgmt.dao.master.TmBasLocDao;
import com.efast.dmom.mgmt.entity.les.*;
import com.efast.dmom.mgmt.entity.master.TmBasLoc;
import com.efast.dmom.mgmt.service.les.TwInvPacktranDetailService;
import com.efast.dmom.mgmt.utils.Assist;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * @author chlp
 */
@Service("twInvPacktranDetailServiceImpl")
public class TwInvPacktranDetailServiceImpl implements TwInvPacktranDetailService {

	private final TwInvPacktranDetailDao twInvPacktranDetailDao;
	private final TwInvPacktranHeaderDao twInvPacktranHeaderDao;
	/**
	 * 库存表
	 */
	private final TwInvSkuLoctlDao twInvSkuLoctlDao;
	/**
	 * 补货任务需求 TW_INV_PAL_MRI
	 */
	private final TwInvPalMriDao twInvPalMriDao;
	/**
	 * 零件仓储关系以及动态库位入库选择规则 tr_bas_part_storage
	 */
	private final TrBasPartStorageDao trBasPartStorageDao;
	@Qualifier("jdbcDao")
	@Autowired
	private JdbcDao jdbcDao;
	/**
	 * 拣货任务表 TW_OUTB_TASK_HEADER
	 */
	private final TwOutbTaskHeaderDao twOutbTaskHeaderDao;
	/**
	 * 拣货任务表 TW_OUTB_TASK_DETAIL
	 */
	private final TwOutbTaskDetailDao twOutbTaskDetailDao;
	/**
	 * 分配表 TW_ACT_ALLOC_LOT_D
	 */
	private final TwActAllocLotdDao twActAllocLotdDao;
	/**
	 * 库位表 tm_bas_loc
	 */
	private final TmBasLocDao tmBasLocDao;

	@Autowired
	public TwInvPacktranDetailServiceImpl(TwInvPacktranDetailDao twInvPacktranDetailDao,
										  TwInvPacktranHeaderDao twInvPacktranHeaderDao, TwInvSkuLoctlDao twInvSkuLoctlDao, TwInvPalMriDao twInvPalMriDao, TrBasPartStorageDao trBasPartStorageDao,
										  TwOutbTaskHeaderDao twOutbTaskHeaderDao, TwOutbTaskDetailDao twOutbTaskDetailDao,
										  TwActAllocLotdDao twActAllocLotdDao, TmBasLocDao tmBasLocDao) {
		this.twInvPacktranDetailDao = twInvPacktranDetailDao;
		this.twInvPacktranHeaderDao = twInvPacktranHeaderDao;
		this.twInvSkuLoctlDao = twInvSkuLoctlDao;
		this.twInvPalMriDao = twInvPalMriDao;
		this.trBasPartStorageDao = trBasPartStorageDao;
		this.twOutbTaskHeaderDao = twOutbTaskHeaderDao;
		this.twOutbTaskDetailDao = twOutbTaskDetailDao;
		this.twActAllocLotdDao = twActAllocLotdDao;
		this.tmBasLocDao = tmBasLocDao;
	}

	/**
	 * 补货完成 : 先变成拣货完成 然后变成补货完成
	 */
	@Override
	public void replenishmentFinish(TwInvPacktranDetail twInvPacktranDetail) {
		TwInvPacktranDetail twInvPacktranDetail1 = twInvPacktranDetailDao.selectTwInvPacktranDetailById(
				twInvPacktranDetail.getTwInvPacktranDetailId());
		if (twInvPacktranDetail1.getPFromLocId() == null || "0".equals(twInvPacktranDetail1.getAcallFlag())) {
			throw new ServiceException(twInvPacktranDetail1.getPartNoVersion() + "没有补货来源库位，请先分配");
		}
		List<TwInvSkuLoctl> list = twInvSkuLoctlDao.selectTwInvSkuLoctl(
				new Assist(Assist.andEq("tm_bas_storage_id", twInvPacktranDetail1.getPFromLocId()),
						Assist.andEq("part_no_version", twInvPacktranDetail1.getPartNoVersion())));
		if (list.size() == 0) {
			TmBasLoc selectTmBasLocById = tmBasLocDao.selectTmBasLocById(twInvPacktranDetail1.getPFromLocId());
			throw new ServiceException(twInvPacktranDetail1.getPartNoVersion() + "分配的补货来源的库位"
					+ selectTmBasLocById.getStorageNo() + "在库存表中没有数据");
		}
		if (list.get(0).getCurQty() < twInvPacktranDetail1.getPacktranPacknum()) {
			throw new ServiceException(list.get(0).getLocNo()+"分配的库位库存不足");
		}
		// 【1】：拣货完成 ：补货任务明细表 - tw_inv_packtran_detail，以及部分拣货字段更新。
		// 0:新任务，1：任务确认,2:部分分配，3：分配完成，4：分配失败，5：部分拣货，6拣货完成，7任务完成，8：关闭，9：补货失败
		TwInvPacktranDetail twInvPacktranDetail2 = new TwInvPacktranDetail();
		twInvPacktranDetail2.setTwInvPacktranDetailId(twInvPacktranDetail1.getTwInvPacktranDetailId());
		// 实际拣货补货数量--< 要求拣货补货数量
		twInvPacktranDetail2.setPickPacktranQty(twInvPacktranDetail1.getPrePacktranQty());
		// 实际拣货补货包装数--<要求补货包装包装数
		twInvPacktranDetail2.setPickPacktranPacknum(twInvPacktranDetail1.getPacktranPacknum());
		// 实际拣货补货包装代码 --< 要求补货包装代码
		twInvPacktranDetail2.setPickPacktranPackno(twInvPacktranDetail1.getPacktranPackno());
		twInvPacktranDetail2.setTaskStatue("6");
		twInvPacktranDetailDao.updateNonEmptyTwInvPacktranDetailById(twInvPacktranDetail2);

		// 【2】：任务完成：补货任务明细表 - tw_inv_packtran_detail，以及部分拣货字段更新。
		TwInvPacktranDetail twInvPacktranDetail3 = new TwInvPacktranDetail();
		twInvPacktranDetail3.setTwInvPacktranDetailId(twInvPacktranDetail1.getTwInvPacktranDetailId());
		// 实际补货补货数量--< 要求拣货补货数量
		twInvPacktranDetail3.setActPacktranQty(twInvPacktranDetail1.getPickPacktranQty());
		// 实际补货补货包装数--<要求补货包装包装数
		twInvPacktranDetail3.setActPacktranPacknum(twInvPacktranDetail1.getPickPacktranPacknum());
		// 实际补货补货包装代码 --< 要求补货包装代码
		twInvPacktranDetail3.setActPacktranPackno(twInvPacktranDetail1.getPickPacktranPackno());
		twInvPacktranDetail3.setTaskStatue("7");
		twInvPacktranDetailDao.updateNonEmptyTwInvPacktranDetailById(twInvPacktranDetail2);

		// 【2】:更新头表状态
		TwInvPacktranHeader twInvPacktranHeader = new TwInvPacktranHeader();
		twInvPacktranHeader.setTwInvPacktranHeaderId(twInvPacktranDetail1.getTwInvPacktranHeaderId());
		// 任务状态（新任务0，任务确认1，任务完成2，任务部分完成3，关闭4）
		twInvPacktranHeader.setPacktranStatus("2");
		twInvPacktranHeaderDao.updateNonEmptyTwInvPacktranHeaderById(twInvPacktranHeader);

		// 【3】：补货任务需求 TW_INV_PAL_MRI，更新成拣货完成，以及部分拣货字段更新。
		List<TwInvPalMri> listTwInvPalMri = twInvPalMriDao.selectTwInvPalMri(
				new Assist(Assist.andEq("tw_inv_packtran_header_id", twInvPacktranDetail1.getTwInvPacktranHeaderId()),
						Assist.andEq("tw_inv_packtran_detail_id", twInvPacktranDetail1.getTwInvPacktranDetailId())));
		if (listTwInvPalMri.size() == 0) {
			throw new ServiceException("没有查询到对应的补货任务需要");
		}
		TwInvPalMri twInvPalMri = new TwInvPalMri();
		twInvPalMri.setTwInvPalMriId(listTwInvPalMri.get(0).getTwInvPalMriId());
		// 实际补货数量
		twInvPalMri.setActRepQty(twInvPacktranDetail1.getPrePacktranQty());
		// 0:未组织;1已组织2:补货任务完成3:关闭
		twInvPalMri.setMriStatue("2");
		twInvPalMriDao.updateNonEmptyTwInvPalMriById(twInvPalMri);

		// 【4】：补货完成，然后扣库存
		TwInvSkuLoctl twInvSkuLoctl = new TwInvSkuLoctl();
		twInvSkuLoctl.setTwInvSkuLoctlId(list.get(0).getTwInvSkuLoctlId());
		twInvSkuLoctl.setCurQty(list.get(0).getCurQty() - twInvPacktranDetail1.getPrePacktranQty());
		twInvSkuLoctl.setAvailableQty(list.get(0).getAvailableQty() - twInvPacktranDetail1.getPrePacktranQty());
		twInvSkuLoctlDao.updateNonEmptyTwInvSkuLoctlById(twInvSkuLoctl);
	}

	/**
	 * 扣除库存
	 *
	 * @param listTwInvSkuLoctl
	 *            可以扣库的记录
	 * @param prePacktranQty
	 *            要求补货包装数量
	 */
	private void deductingInventory(List<TwInvSkuLoctl> listTwInvSkuLoctl, Integer prePacktranQty) {
		// 如果一条记录就满足，需要扣减的数量<一条记录的可用库存
		if (prePacktranQty < listTwInvSkuLoctl.get(0).getCurQty()) {
			System.out.println();
			TwInvSkuLoctl twInvSkuLoctl3 = new TwInvSkuLoctl();
			twInvSkuLoctl3.setTwInvSkuLoctlId(listTwInvSkuLoctl.get(0).getTwInvSkuLoctlId());
			twInvSkuLoctl3.setCurQty(listTwInvSkuLoctl.get(0).getCurQty() - prePacktranQty);
			twInvSkuLoctl3.setAvailableQty(listTwInvSkuLoctl.get(0).getAvailableQty() - prePacktranQty);
			twInvSkuLoctlDao.updateNonEmptyTwInvSkuLoctlById(twInvSkuLoctl3);
			// 【3】：扣库完成后，需要写入出库记录(tt_whm_stock_outbound)，供后续库存追溯
			// insertNonEmptyTtWhmStockOutbound(twInvPacktranDetail, listTwInvSkuLoctl);
		} else {
			System.out.println();
			// 不满足，则循环扣库
			double sumQty = 0;
			for (TwInvSkuLoctl twInvSkuLoctl : listTwInvSkuLoctl) {
				sumQty += twInvSkuLoctl.getAvailableQty();
			}
			if (prePacktranQty > sumQty) {
				throw new ServiceException(listTwInvSkuLoctl.get(0).getPartNoVersion() + "当前可用库存小于消耗");
			}
			for (TwInvSkuLoctl twInvSkuLoctl : listTwInvSkuLoctl) {
				if (prePacktranQty > twInvSkuLoctl.getAvailableQty()) {
					TwInvSkuLoctl twInvSkuLoctl3 = new TwInvSkuLoctl();
                    System.out.println();
					twInvSkuLoctl3.setTwInvSkuLoctlId(twInvSkuLoctl.getTwInvSkuLoctlId());
					twInvSkuLoctl3.setCurQty(0);
					twInvSkuLoctl3.setAvailableQty(0);
					twInvSkuLoctlDao.updateNonEmptyTwInvSkuLoctlById(twInvSkuLoctl3);
					// 【3】：扣库完成后，需要写入出库记录(tt_whm_stock_outbound)，供后续库存追溯
					// insertNonEmptyTtWhmStockOutbound(twInvPacktranDetail, listTwInvSkuLoctl);
				} else {
					TwInvSkuLoctl twInvSkuLoctl3 = new TwInvSkuLoctl();
					twInvSkuLoctl3.setTwInvSkuLoctlId(twInvSkuLoctl.getTwInvSkuLoctlId());
					twInvSkuLoctl3.setCurQty(twInvSkuLoctl.getAvailableQty() - prePacktranQty);
					twInvSkuLoctl3.setAvailableQty(twInvSkuLoctl.getAvailableQty() - prePacktranQty);
					twInvSkuLoctlDao.updateNonEmptyTwInvSkuLoctlById(twInvSkuLoctl3);
					// 【3】：扣库完成后，需要写入出库记录(tt_whm_stock_outbound)，供后续库存追溯
					// insertNonEmptyTtWhmStockOutbound(twInvPacktranDetail, listTwInvSkuLoctl);
					break;
				}
				prePacktranQty = prePacktranQty - twInvSkuLoctl.getAvailableQty();
			}
		}
	}

	/**
	 * 补货完成 -- 没有分配库位的情况
	 */
	@Override
	public void replenishmentFinishNotAllot(TwInvPacktranDetail twInvPacktranDetail) {

		// 0:新任务，1：任务确认,2:部分分配，3：分配完成，4：分配失败，5：部分拣货，6拣货完成，7任务完成，8：关闭，9：补货失败
		List<TwOutbTaskHeader> listTwOutbTaskHeader = twOutbTaskHeaderDao.selectTwOutbTaskHeader(
				new Assist(Assist.andEq("so_sheet_no", twInvPacktranDetail.getPacktranSheetNo())));

		List<TwOutbTaskDetail> selectTwOutbTaskDetail = twOutbTaskDetailDao.selectTwOutbTaskDetail(new Assist(
				Assist.andEq("tw_outb_task_header_id", listTwOutbTaskHeader.get(0).getTwOutbTaskHeaderId())));

		// 拣货状态 0新任务 1拣货完成 2部分拣货 3关闭
		if ("1".equals(listTwOutbTaskHeader.get(0).getInboundStatus())
				|| "2".equals(listTwOutbTaskHeader.get(0).getInboundStatus())) {

			// 【2】：任务完成：补货任务明细表 - tw_inv_packtran_detail，以及部分拣货字段更新。
			TwInvPacktranDetail twInvPacktranDetail3 = new TwInvPacktranDetail();
			twInvPacktranDetail3.setTwInvPacktranDetailId(twInvPacktranDetail.getTwInvPacktranDetailId());
			// 实际补货补货数量--< 要求拣货补货数量
			twInvPacktranDetail3.setActPacktranQty(selectTwOutbTaskDetail.get(0).getActPickQty());
			// 实际补货补货包装数--<
			twInvPacktranDetail3.setActPacktranPacknum(selectTwOutbTaskDetail.get(0).getActPickPackNum());
			// 要求补货包装包装数
			// 实际补货补货包装代码 --<
			twInvPacktranDetail3.setActPacktranPackno(selectTwOutbTaskDetail.get(0).getPreAllocPack());
			// 要求补货包装代码
			twInvPacktranDetail3.setTaskStatue("7");
			twInvPacktranDetailDao.updateNonEmptyTwInvPacktranDetailById(twInvPacktranDetail3);

			// 【3】：补货任务需求 TW_INV_PAL_MRI，更新成拣货完成，以及部分拣货字段更新。
			List<TwInvPalMri> listTwInvPalMri = twInvPalMriDao.selectTwInvPalMri(new Assist(
					Assist.andEq("tw_inv_packtran_header_id", twInvPacktranDetail.getTwInvPacktranHeaderId()),
					Assist.andEq("tw_inv_packtran_detail_id", twInvPacktranDetail.getTwInvPacktranDetailId())));
			TwInvPalMri twInvPalMri = new TwInvPalMri();
			twInvPalMri.setTwInvPalMriId(listTwInvPalMri.get(0).getTwInvPalMriId());
			// 实际补货数量
			twInvPalMri.setActRepQty(selectTwOutbTaskDetail.get(0).getActPickQty());
			// 0:未组织;1已组织2:补货任务完成3:关闭
			twInvPalMri.setMriStatue("2");
			twInvPalMriDao.updateNonEmptyTwInvPalMriById(twInvPalMri);

			// 【4】：补货完成，然后扣库存
			List<TwInvSkuLoctl> list = twInvSkuLoctlDao.selectTwInvSkuLoctl(
					new Assist(Assist.andEq("tm_bas_storage_id", selectTwOutbTaskDetail.get(0).getPreTmBasStorageId()),
							Assist.andEq("part_no_version", twInvPacktranDetail.getPartNoVersion())));
			TwInvSkuLoctl twInvSkuLoctl = new TwInvSkuLoctl();
			twInvSkuLoctl.setTwInvSkuLoctlId(list.get(0).getTwInvSkuLoctlId());
			twInvSkuLoctl.setCurQty(list.get(0).getCurQty() - selectTwOutbTaskDetail.get(0).getActPickQty());
			twInvSkuLoctl.setAvailableQty(
					list.get(0).getAvailableQty() - selectTwOutbTaskDetail.get(0).getActPickQty());
			twInvSkuLoctlDao.updateNonEmptyTwInvSkuLoctlById(twInvSkuLoctl);
		} else {
			throw new ServiceException("必须先部分拣货或拣货完成才能完成任务");
		}
	}

	/**
	 * 按单分配
	 *
	 * @see com.efast.emfg.remote.les.TwInvPacktranDetailService#distribution(com.efast.emfg.po.les.TwInvPacktranDetail)
	 */
	@Override
	public void distribution(TwInvPacktranDetail twInvPacktranDetail) {

		// 【1】：根据零件号 去 零件仓库关系（tr_bas_part_storage）找一个库位
		List<TrBasPartStorage> listTrBasPartStorage = trBasPartStorageDao.selectTrBasPartStorage(new Assist(
				Assist.andEq("tr_bas_part_storage.part_no_version", twInvPacktranDetail.getPartNoVersion())));

		// 【2】：查询改库存是否存在，以及库存是否够
		List<TwInvSkuLoctl> listTwInvSkuLoctl = twInvSkuLoctlDao.selectTwInvSkuLoctl(
				new Assist(Assist.andEq("tw_inv_sku_loc_t_l.tm_bas_storage_id", listTrBasPartStorage.get(0).getLocId()),
						Assist.andEq("tw_inv_sku_loc_t_l.part_no_version", twInvPacktranDetail.getPartNoVersion())));
		if (listTwInvSkuLoctl.size() == 0) {
			// 分配的库位在库存表中没有记录
			throw new ServiceException("库存表中没有零件=" + twInvPacktranDetail.getPartNoVersion() + "并且库位id="
					+ listTrBasPartStorage.get(0).getLocId());
		}
		TwInvSkuLoctl twInvSkuLoctl = listTwInvSkuLoctl.get(0);
		if(twInvPacktranDetail.getActPacktranQty()==null){
			twInvPacktranDetail.setActPacktranQty(0);
		}
		if (twInvSkuLoctl.getAvailableQty() < twInvPacktranDetail.getActPacktranQty()) {
			throw new ServiceException(twInvPacktranDetail.getPartNoVersion() + "分配的库位库存不足");
		}

		// 【3】：生成 拣货任务表 TW_OUTB_TASK_HEADER
		TwOutbTaskHeader twOutbTaskHeader = new TwOutbTaskHeader();
		// 拣货状态0新任务1拣货完成2部分拣货3
		twOutbTaskHeader.setInboundStatus("0");
		twOutbTaskHeader.setSoSheetNo(twInvPacktranDetail.getPacktranSheetNo());
		// 拣货任务清单打印状态（0打印1已打印)
		twOutbTaskHeader.setPrintStatus("0");
		String pickAllocNo = jdbcDao.executeProcGetCode("tw_outb_task_header", "efast");
		// 拣货任务清单单号
		twOutbTaskHeader.setPickAllocNo(pickAllocNo);
		twOutbTaskHeader.setTaskCreateman(SpringWebUtils.getRequestUser());
		twOutbTaskHeader.setTaskCreatetime(new Date());
		twOutbTaskHeader.setMarkStatus(1);
		twOutbTaskHeader.setCompanyCode(SpringWebUtils.getRequestCompany());
		twOutbTaskHeader.setCreateUser(SpringWebUtils.getRequestUser());
		twOutbTaskHeader.setCreateTime(new Date());
		twOutbTaskHeaderDao.insertNonEmptyTwOutbTaskHeader(twOutbTaskHeader);

		// 【4】：生成 拣货任务表 明细 TW_OUTB_TASK_DETAIL
		// 拣货任务表 TW_OUTB_TASK_DETAIL
		TwOutbTaskDetail twOutbTaskDetail = new TwOutbTaskDetail();
		// 分配表 TW_ACT_ALLOC_LOT_D
		TwActAllocLotd twActAllocLotd = new TwActAllocLotd();
		BeanUtils.copyProperties(twInvPacktranDetail, twOutbTaskDetail);
		twOutbTaskDetail.setTwOutbTaskHeaderId(twOutbTaskHeader.getTwOutbTaskHeaderId());
		twOutbTaskDetail.setSoLineno(1);
		// 待拣货仓储单元id
		twOutbTaskDetail.setPreTmBasStorageId(listTrBasPartStorage.get(0).getLocId());
		// 要求分配数量 <--要求发货数量(soReqQty)
		twOutbTaskDetail.setPreAllocQty(twInvPacktranDetail.getPrePacktranQty());
		// 实际分配数量 <--要求发货数量(soReqQty)
		twOutbTaskDetail.setAllocQty(twInvPacktranDetail.getPrePacktranQty());
		// 要求分配包装数
		twOutbTaskDetail.setPreAllocPackNum(twInvPacktranDetail.getPacktranPacknum());
		// 实际分配包装数量
		twOutbTaskDetail.setAllocPackNum(twInvPacktranDetail.getPacktranPacknum());
		// 要求分配包装代码
		twOutbTaskDetail.setPreAllocPack(twInvPacktranDetail.getPacktranPackno());
		// 实际分配包装代码
		twOutbTaskDetail.setAllocPack(twInvPacktranDetail.getPacktranPackno());
		// 分配类型（0出库订单分配1翻包补货分配2补货分配）
		twOutbTaskDetail.setAllocType("0");
		// 拣货任务标签打印状态（0打印 1已打印）
		twOutbTaskDetail.setPrintStatus("0");
		// 任务标签打印状态（0打印 1已打印）
		twOutbTaskDetail.setTaskPrintStatus("0");
		// 分配明细id
		twOutbTaskDetail.setTwActAllocLotdId(twActAllocLotd.getTwActAllocLotdId());
		// 拣货状态（新任务0，任务完成1，2部分拣货）
		twOutbTaskDetail.setPickStatus("0");
		twOutbTaskDetail.setLineno(1);
		// 拣货任务清单单号
		twOutbTaskDetail.setPickAllocNo(pickAllocNo);
		twOutbTaskDetail.setDestLocId(listTrBasPartStorage.get(0).getLocId());
		// 库内跟踪流水号码
		twOutbTaskDetail.setInbSerialNo(twInvSkuLoctl.getInbSerialNo());
		// 批次号
		twOutbTaskDetail.setLotNo(twInvSkuLoctl.getLotNo());
		// 零件包装流水号
		twOutbTaskDetail.setPartPackSerial(twInvSkuLoctl.getPartPackSerial());
		// 出库时间
		twOutbTaskDetail.setOutboundTime(twInvPacktranDetail.getTaskCreateTime());
		// 出库人
		twOutbTaskDetail.setOutboundMan(SpringWebUtils.getRequestUser());
		twOutbTaskDetail.setMarkStatus(1);
		twOutbTaskDetail.setCreateTime(new Date());
		twOutbTaskDetail.setCreateUser(SpringWebUtils.getRequestUser());
		twOutbTaskDetail.setCompanyCode(SpringWebUtils.getRequestCompany());
		twOutbTaskDetailDao.insertNonEmptyTwOutbTaskDetail(twOutbTaskDetail);

		// 【5】：向分配表 TW_ACT_ALLOC_LOT_D 插入数据
		BeanUtils.copyProperties(twInvPacktranDetail, twActAllocLotd);
		// 待拣货仓储单元ID
		twActAllocLotd.setPreTmBasStorageId(listTrBasPartStorage.get(0).getLocId());
		// 实际拣货仓储单元ID
		twActAllocLotd.setActTmBasStorageId(listTrBasPartStorage.get(0).getLocId());
		twActAllocLotd.setPartNoVersion(twInvPacktranDetail.getPartNoVersion());
		twActAllocLotd.setLotNo(twInvSkuLoctl.getLotNo());
		// 分配类型（0出库订单分配1翻包补货分配2补货分配）
		twActAllocLotd.setAllocType("0");
		// 装箱标签打印状态（0打印1已打印）
		twActAllocLotd.setTaskPrintStatus("0");
		// 状态（新建0、拣货任务产生1、部分拣货2、拣货完成3、关闭4）
		twActAllocLotd.setItemPickStatus("1");
		twActAllocLotd.setTwOutbTaskDetailId(twOutbTaskDetail.getTwOutbTaskDetailId());
		// 装箱任务行号ID
		twActAllocLotd.setTwOutbTaskHeaderId(twOutbTaskHeader.getTwOutbTaskHeaderId());
		// 状态（发运任务未产生0、发运任务产生1、部分发运2、发运完成3）
		twActAllocLotd.setItemLoadStatus("0");
		// 实际分配数量
		twActAllocLotd.setAllocQty(twInvPacktranDetail.getPrePacktranQty());
		// 要求分配数量
		twActAllocLotd.setPreAllocQty(twInvPacktranDetail.getPrePacktranQty());
		// 实际分配包装数量
		twActAllocLotd.setAllocPackNum(twInvPacktranDetail.getPacktranPacknum());
		// 要求分配包装数量
		twActAllocLotd.setPreAllocPackNum(twInvPacktranDetail.getPacktranPacknum());
		// 实际分配供应商编号
		twActAllocLotd.setActSupplNo(twInvPacktranDetail.getSupplNo());
		// 实际分配包装容量
		twActAllocLotd.setAllocPackageQty(twInvPacktranDetail.getPacktranPackqty());
		// 要求分配包装容量
		twActAllocLotd.setPreAllocPackageQty(twInvPacktranDetail.getPacktranPackqty());
		twActAllocLotd.setMarkStatus(1);
		twActAllocLotd.setCreateTime(new Date());
		twActAllocLotd.setCreateUser(SpringWebUtils.getRequestUser());
		twActAllocLotd.setCompanyCode(SpringWebUtils.getRequestCompany());
		twActAllocLotdDao.insertNonEmptyTwActAllocLotd(twActAllocLotd);

		// 【6】：更新 补货任务明细TW_INV_PACKTRAN_DETAIL
		twInvPacktranDetail.setPFromLocId(listTrBasPartStorage.get(0).getLocId());
		twInvPacktranDetail.setAcallFlag("1");
		twInvPacktranDetailDao.updateNonEmptyTwInvPacktranDetailById(twInvPacktranDetail);
	}

	@Override
	public long getTwInvPacktranDetailRowCount(Assist assist) {
		return twInvPacktranDetailDao.getTwInvPacktranDetailRowCount(assist);
	}

	@Override
	public List<TwInvPacktranDetail> selectTwInvPacktranDetail(Assist assist) {
		return twInvPacktranDetailDao.selectTwInvPacktranDetail(assist);
	}

	@Override
	public TwInvPacktranDetail selectTwInvPacktranDetailByObj(TwInvPacktranDetail obj) {
		return twInvPacktranDetailDao.selectTwInvPacktranDetailByObj(obj);
	}

	@Override
	public TwInvPacktranDetail selectTwInvPacktranDetailById(Integer id) {
		return twInvPacktranDetailDao.selectTwInvPacktranDetailById(id);
	}

	@Override
	public int insertTwInvPacktranDetail(TwInvPacktranDetail value) {
		return twInvPacktranDetailDao.insertTwInvPacktranDetail(value);
	}

	@Override
	public int insertNonEmptyTwInvPacktranDetail(TwInvPacktranDetail value) {
		return twInvPacktranDetailDao.insertNonEmptyTwInvPacktranDetail(value);
	}

	@Override
	public int insertTwInvPacktranDetailByBatch(List<TwInvPacktranDetail> value) {
		return twInvPacktranDetailDao.insertTwInvPacktranDetailByBatch(value);
	}

	@Override
	public int deleteTwInvPacktranDetailById(Integer id) {
		return twInvPacktranDetailDao.deleteTwInvPacktranDetailById(id);
	}

	@Override
	public int deleteTwInvPacktranDetail(Assist assist) {
		return twInvPacktranDetailDao.deleteTwInvPacktranDetail(assist);
	}

	@Override
	public int updateTwInvPacktranDetailById(TwInvPacktranDetail enti) {
		return twInvPacktranDetailDao.updateTwInvPacktranDetailById(enti);
	}

	@Override
	public int updateTwInvPacktranDetail(TwInvPacktranDetail value, Assist assist) {
		return twInvPacktranDetailDao.updateTwInvPacktranDetail(value, assist);
	}

	@Override
	public int updateNonEmptyTwInvPacktranDetailById(TwInvPacktranDetail enti) {
		return twInvPacktranDetailDao.updateNonEmptyTwInvPacktranDetailById(enti);
	}

	@Override
	public int updateNonEmptyTwInvPacktranDetail(TwInvPacktranDetail value, Assist assist) {
		return twInvPacktranDetailDao.updateNonEmptyTwInvPacktranDetail(value, assist);
	}
	@Override
	public List<Integer> queryTrBasDpathSeq(String dpathNo) {
		return twInvPacktranDetailDao.queryTrBasDpathSeq(dpathNo);
	}
	@Override
	public List<PdaQueryTwInvPacktranDetail> selectPdaTwInvPacktranDetail(Assist assist) {
		return twInvPacktranDetailDao.selectPdaTwInvPacktranDetail(assist);
	}
}